package util

import (
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/text/gstr"
	"github.com/mojocn/base64Captcha"
)

//Version 版本号
const Version = "1.1.03"

// //获取数字验证码
// func GetVerifyImgDigit() (idKeyC string, base64stringC string) {
// 	driver := &base64Captcha.DriverDigit{80, 240, 5, 0.7, 5}
// 	store := base64Captcha.DefaultMemStore
// 	c := base64Captcha.NewCaptcha(driver, store)
// 	idKeyC, base64stringC, err := c.Generate()
// 	if err != nil {
// 		g.Log().Error(err)
// 	}
// 	return
// }

//GetVerifyImgString 获取字母数字混合验证码
func GetVerifyImgString() (idKeyC string, base64stringC string) {
	driver := &base64Captcha.DriverString{
		Height:          80,
		Width:           240,
		NoiseCount:      50,
		ShowLineOptions: 20,
		Length:          4,
		Source:          "abcdefghjkmnpqrstuvwxyz23456789",
		Fonts:           []string{"chromohv.ttf"},
	}
	driver = driver.ConvertFonts()
	store := base64Captcha.DefaultMemStore
	c := base64Captcha.NewCaptcha(driver, store)
	idKeyC, base64stringC, err := c.Generate()
	if err != nil {
		g.Log().Error(err)
	}
	return
}

//VerifyString 验证输入的验证码是否正确
func VerifyString(id, answer string) bool {
	driver := new(base64Captcha.DriverString)
	store := base64Captcha.DefaultMemStore
	c := base64Captcha.NewCaptcha(driver, store)
	answer = gstr.ToLower(answer)
	return c.Verify(id, answer, true)
}

// //AuthAfterFunc gtoken验证后返回
// func AuthAfterFunc(r *ghttp.Request, respData gtoken.Resp) {
// 	if r.Method == "OPTIONS" || respData.Success() {
// 		r.Middleware.Next()
// 	} else {
// 		respData.Msg = "用户信息验证失败"
// 		response := r.Response
// 		options := response.DefaultCORSOptions()
// 		response.CORS(options)
// 		response.WriteJson(respData)
// 		r.ExitAll()
// 	}
// }

// //loginLog 登录日志记录
// func loginLog(status int, username, ip, userAgent, msg, module string) {
// 	var log sys_login_log.Entity
// 	log.LoginName = username
// 	log.Ipaddr = ip
// 	log.LoginLocation = utils.GetCityByIp(log.Ipaddr)
// 	ua := user_agent.New(userAgent)
// 	log.Browser, _ = ua.Browser()
// 	log.Os = ua.OS()
// 	log.Status = status
// 	log.Msg = msg
// 	log.LoginTime = gtime.Timestamp()
// 	log.Module = module
// 	log.Save()
// }
